package com.milepost.workflow.testsync;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LockUtil {

    private static Map<String, String> lockKeyMap = new ConcurrentHashMap<>();

    public static boolean getLock(String lockKey) {
        synchronized (lockKey.intern()) {
            if (lockKeyMap.containsKey(lockKey)) {
                //锁被占用
                return false;
            } else {
                //获取锁成功
                lockKeyMap.put(lockKey, lockKey);
                return true;
            }
        }
    }

    public static void releaseLock(String lockKey) {
        synchronized (lockKey.intern()) {
            lockKeyMap.remove(lockKey);
        }
    }
}
